package com.example.nutritionmanager.home

import android.content.Intent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.example.nutritionmanager.ocr.CameraActivity

@Composable
fun HomeScreen(
    navController: NavController,
) {
    val context = LocalContext.current  // 获取当前上下文

    // 使整个页面可上下滑动
    Column(
        modifier = Modifier
            .fillMaxSize()
            .verticalScroll(rememberScrollState()) // 使页面可滑动
//            .background(Color(0xFFf1f1f1))  // 设置背景为灰色
            .background(MaterialTheme.colorScheme.background)  // 设置背景为灰色
            .padding(16.dp) // 页面内容的内边距
    ) {
        Text(
            text = "首页",
            fontSize = 22.sp,
            color = MaterialTheme.colorScheme.onBackground,
            fontWeight = FontWeight.Bold,
            modifier = Modifier.padding(bottom = 16.dp)
        )
        // 1. 拍照识别按钮
        PhotoRecognitionButton(onClick = {
            val intent = Intent(context, CameraActivity::class.java)
            context.startActivity(intent)
        })

        Spacer(modifier = Modifier.height(24.dp))

        // 2. 饮食仪表盘
        IntakeDashboard(
//                    modifier = Modifier.align(Alignment.CenterHorizontally)
            modifier = Modifier.fillMaxWidth()
        )

        // 3. 添加记录按钮
        Spacer(modifier = Modifier.height(24.dp))
        AddMealButtons(navController)


        // 4. 最近七天卡路里摄入量图表
        Spacer(modifier = Modifier.height(24.dp))
        CalorieBarChart()

        Spacer(modifier = Modifier.height(48.dp))

    }
}


@Preview(showBackground = true)
@Composable
fun PreviewHomeScreen() {
    HomeScreen(
        navController = rememberNavController(),
    )
}
